var Md5 = require('../../utils/md5.js');
var timeSecond = Math.floor(Date.now() * 0.001);  //获取时间戳: 
var SignInfoData = Md5.hexMD5(timeSecond + 'nativetalk')//md5加密;
var WxParse = require('../../wxParse/wxParse.js');
var teacherDataArr = [], commentDataArr = [];
var tea_PageNum = 1, con_PageNum = 1, NavId, classId, set_id, set_amout, commentTotalPage, teacherTotalPage;
const app = getApp();
var httpNativetalk = app.globalData.httpNativetalk;
var httpWeike = app.globalData.httpWeike;
Page({
  data: {
    NavData: ['简介', '课表', '老师'],
    NavComment: ['评价'],
    NavId: 0,
    buyClassFlag: false,
    imgUrl: 'http://file.waijiaojun.com/waijiaojun/icon',
    isVideo: false,
  },
  onShow:function(){
    wx.showShareMenu({
      withShareTicket: true,
    })
  },
  onLoad: function (e) {
    var that = this;
    classId = e.classId;
    NavId = e.NavId;
    that.setData({
      NavId: NavId,
    });  
    //课程简介与课表    
    wx.request({
      url: httpNativetalk + '/app/7/course/index',
      data: {
        "cou_id": e.classId,
      },
      header: {//请求头
        "Timestamp": timeSecond,
        "SignInfo": SignInfoData,
        "content-Type": "application/json",
        "client_type": "3",
        "version": 15,
        "token": '',
      },
      method: 'POST',
      success: function (res) {
        //课程简介与课表请求结果
        // console.log(res);
        var classIntroData = res.data.obj.course;
        var VideoPicture = 'http://file.waijiaojun.com/' + classIntroData.cou_video_picture;
        var NavComment = [];
        var cou_introduce = classIntroData.cou_introduce;
        var levelDetail = res.data.obj.levelDetail;
        var level = res.data.obj.level;
        var aa = classIntroData.video_url;        
        if (aa == null || aa == '') {
          that.setData({
            isVideo: true,
          })
        };
        if (res.data.obj.levelDetail.length == 0) {
          that.setData({ classContentNull: true })
        } else { that.setData({ classContentNull: false }) };

        WxParse.wxParse('cou_introduce', 'html', cou_introduce, that, 0);
        if (res.data.obj.comment == [] || res.data.obj.comment == '') {
          res.data.obj.comment.length = 0;
        };
        var ClassNamesData = [];
        var levelData = [];
        for (var i = 0; i < levelDetail.length; i++) {
          ClassNamesData.push(levelDetail[i].level_name)
        }
        for (var j = 0; j < ClassNamesData.length; j++) {
          levelData.push(level[ClassNamesData[j]]);
        }
        // console.log(levelData);
        // NavComment.push('评价(' + res.data.obj.comment.length + ')')
        NavComment.push('评价')
        that.setData({
          classIntroData: classIntroData,
          levelDetail: res.data.obj.levelDetail,
          NavComment: NavComment,
          VideoPicture: VideoPicture,
          levelData: levelData,
          commentNum: res.data.obj.comment.length,
          classId: e.classId,
        })
      },
      fail: function (res) { },
      complete: function (res) { },
    })
    //老师数据请求
    that.teacherDataFun(classId, tea_PageNum);
    //评论数据请求    
    that.commentDataFun(classId, con_PageNum);

  },  
  onUnload: function () {

  },
  //老师数据请求函数
  teacherDataFun: function (classId, tea_PageNum) {
    var that = this;
    wx.request({
      url: httpNativetalk + '/app/11/search/course/teacher',
      data: {
        obj: classId,
        "currentPage": tea_PageNum,
      },
      header: {//请求头
        "Timestamp": timeSecond,
        "SignInfo": SignInfoData,
        "content-Type": "application/json",
        "client_type": "3",
        "version": 15,
        "token": '',
      },
      method: 'POST',
      success: function (res) {
        // console.log(res);
        teacherTotalPage = res.data.obj.tea_totalPage;
        // console.log(teacherTotalPage);
        if (res.data.obj.teachers == null) {
          that.setData({
            teacherListNull: true,
            teacherData: res.data.obj.teachers,
          })
        } else {
          teacherDataArr.push(res.data.obj.teachers)
          // console.log(teacherDataArr)
          that.setData({
            teacherData: res.data.obj.teachers,
            teacherDataArr: teacherDataArr,
            teacherTotalPage: teacherTotalPage,
          })
        }

      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  //评论数据请求函数
  commentDataFun: function (classId, con_PageNum) {
    var that = this;
    wx.request({
      url: httpNativetalk + '/app/10/course/comment/list',
      data: {
        "currentPage": con_PageNum,
        obj: classId,
      },
      header: {//请求头
        "Timestamp": timeSecond,
        "SignInfo": SignInfoData,
        "content-Type": "application/json",
        "client_type": "3",
        "version": 15,
        "token": '',
      },
      method: 'POST',
      success: function (res) {
        //评价请求结果
        // console.log(res);
        commentDataArr.push(res.data.obj.comments);
        commentTotalPage = res.data.obj.totalPage;
        var replayData = res.data.obj.comments;
        if (replayData == null) {
          that.setData({
            commentFlag: true,
          });
        } else {
          for (var i = 0; i < replayData.length; i++) {
            //时间戳的处理
            replayData[i].com_time = getDate(replayData[i].com_time);
            //评论老师回复
            if (replayData[i].com_reply_content == null || replayData[i].com_reply_content == "") {
              replayData[i].com_reply_content = false;
            };
            //头像连接的处理
            var headImg = replayData[i].memb_head_portrait;
            if (headImg.slice(0, 4) == "/nat") {
              replayData[i].memb_head_portrait = 'http://file.waijiaojun.com' + replayData[i].memb_head_portrait;
            };
            if (headImg.slice(0, 4) == "http") {
              replayData[i].memb_head_portrait = replayData[i].memb_head_portrait;
            };
            if (headImg.slice(0, 4) == "waij") {
              replayData[i].memb_head_portrait = 'http://file.waijiaojun.com/' + replayData[i].memb_head_portrait;
            };
          }
        };
        function getDate(time) {
          var date = new Date(time);
          var Y = date.getFullYear() + '-';
          var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
          var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
          return (Y + M + D);
        };

        that.setData({
          replayData: replayData,
          commentDataArr: commentDataArr,
        })
      },
      fail: function (res) { },
      complete: function (res) { },
    })

  },
  //触底加载函数
  onReachBottom: function () {
    var that = this;
    if (NavId == 2) {
      tea_PageNum++;
      if (tea_PageNum>1&&tea_PageNum <= teacherTotalPage) {
        this.teacherDataFun(classId, tea_PageNum)
      }
    }
    if (NavId == 3) {
      con_PageNum++;
      if (con_PageNum>1&&con_PageNum <= commentTotalPage) {
        this.commentDataFun(classId, con_PageNum)
      }
    }
  },
  //小导航函数
  calssNavFun: function (event) {
    console.log(event.currentTarget.dataset.idx);
    NavId = event.currentTarget.dataset.idx;
    this.setData({
      NavId: event.currentTarget.dataset.idx,
    })
  },
  //购买课程
  buyClassFun: function (e) {
    if (app.globalData.token == null) {
      wx.navigateTo({
        url: '/pages/bindPhone/bindPhone',
      })
    }
    if (app.globalData.token !== null) {
      var that = this;
      wx.request({
        url: httpNativetalk + '/app/11/course/set/choose',
        data: {
          cou_id: e.currentTarget.dataset.idx,
        },
        header: {//请求头
          "Timestamp": timeSecond,
          "SignInfo": SignInfoData,
          "content-Type": "application/json",
          "client_type": "3",
          "version": 15,
          "token": '',
        },
        method: 'POST',
        success: function (res) {
          console.log(res);
          that.setData({
            setMealData: res.data.obj.setmeals
          })
        },
        fail: function (res) { },
        complete: function (res) { },
      });
      this.setData({
        buyClassFlag: true,
      })
    }

  },
  closeLayerFun: function () {
    this.setData({
      buyClassFlag: false,
    })
  },
  layerSizeFun: function (e) {
    console.log(e.currentTarget.dataset);
    set_id = e.currentTarget.dataset.set_id;
    set_amout = e.currentTarget.dataset.set_amout;
    this.setData({
      layerSizeId: e.currentTarget.dataset.idx,
      thisItem: e.currentTarget.dataset.item,
    })
  },
  orderSureFun: function (e) {
    console.log(set_amout)
    if (set_amout == undefined) {
      return;
    }
    wx.navigateTo({
      url: '/pages/classBuy/classBuy?set_id=' + set_id + '&set_amout=' + set_amout + '&classId=' + classId,
    });
  },
  showHiddenFun: function (e) {

  },
  //预约老师,预约前查询是否购买该课程
  teacherAppointFun: function (e) {
    console.log(e);
    var dates = Date.now();
    var teaIds = e.currentTarget.dataset.teaid;
    // var classIds = e.currentTarget.dataset.couid;
    // var AAs='aa';
    wx.navigateTo({
      url: '/pages/selectTime/selectTime?teaId=' + teaIds + '&date=' + dates + '&classId=' + classId,
    });
  },
  //查询是否购买该课程
  queryFun: function (e) {
    if (app.globalData.token !== null) {
      var that = this;
      wx.request({
        url: httpNativetalk + '/app/7/token/book/connect',
        data: {
          "currentPage": 1,
          "pageSize": 10,
        },
        header: {
          "Timestamp": timeSecond,
          "SignInfo": SignInfoData,
          "content-Type": "application/json",
          "client_type": "3",
          "version": 15,
          'token': app.globalData.token,
        },
        method: 'POST',
        success: function (res) {
          console.log(res);
        },
        fail: function (res) { },
        complete: function (res) { },
      })
    } else {
      wx.showToast({
        title: '请先绑定手机号',
        icon: 'loading',
        duration: 2000
      });
    }
  },
  classVideoPlayFun: function (e) {
    wx.navigateTo({
      url: '/pages/classDetailVideoplay/classDetailVideoplay?classId=' + classId,
    });
  },
  onHide: function (e) {   
    this.setData({
      buyClassFlag: false,
    })
  },
  onUnload: function () {
    commentDataArr=[]
  },
})